<?php
namespace Home\Controller;

use Think\Controller;

class CommonController extends Controller
{
    private $category;

    function __construct()
    {
        parent::__construct();
        $this->category = M('category');
        dump($this->nav_bar());
        $this->assign('nav_bar', $this->nav_bar());
    }

    private function nav_bar()
    {
        $Category = D('category');
        $categories = $Category->relation(true)->where('parent_id = 0')->select();

        foreach ($categories as $key => &$value) {
            $this->make_url($value);
            foreach ($value['children'] as &$v) {
                $this->make_url($v);
            }
        }

        return $categories;
    }

    private function make_url(&$category)
    {
        switch ($category['type']) {
            case '1':
                $category['link'] = U('category', array('id' => $category['id']));
                break;
            case '2' :
                $category['link'] = U('list', array('id' => $category['id']));
                break;
            default:
                $category['link'] = $category['url'];
        }
    }

}